package com.xonro.common.histrans.base.biz;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.commons.database.RowMap;
import com.actionsoft.bpms.server.UserContext;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.sdk.local.SDK;
import com.xonro.common.histrans.base.ctx.BOCreateData;
import com.xonro.common.histrans.base.ctx.HistoryTransContext;
import com.xonro.common.histrans.base.ctx.ProcessCreateContext;
import com.xonro.common.histrans.base.model.HistoryTransLogModel;
import jodd.util.StringUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public abstract class ProcessTransBiz {
    protected abstract boolean transProcess(HistoryTransContext historyTransContext);

    protected abstract HistoryTransContext initTransContext();
    public void trans() {
        List<RowMap> mainDatas = getMainDatas();
        SDK.getLogAPI().consoleInfo("所有数据+++++"+mainDatas);
        for (RowMap map : mainDatas) {
            HistoryTransContext historyTransContext = initTransContext();
            historyTransContext.setMainData(map);
            boolean b = this.transProcess(historyTransContext);
            if(b){
                this.perist(historyTransContext);
            }
        }
    }
    public void trans1() {

        RowMap mainDatas = getMainData();
            HistoryTransContext historyTransContext = initTransContext();
            historyTransContext.setMainData(mainDatas);
            boolean b = this.transProcess(historyTransContext);
            if(b){
                this.perist(historyTransContext);
            }
    }
    protected void addLogModel(HistoryTransContext historyTransContext, String outerTable, String pk, String bpmTableName, String bpmId, String bindId){
        HistoryTransLogModel historyTransLogModel = new HistoryTransLogModel();
        historyTransLogModel.setOuterTable(outerTable);
        historyTransLogModel.setOuterPK(pk);
        historyTransLogModel.setBpmTable(bpmTableName);
        historyTransLogModel.setBpmBOId(bpmId);
        historyTransLogModel.setBindId(bindId);
        historyTransContext.setHistoryTransLogModelList(historyTransLogModel);
    }
    protected abstract List<RowMap> getMainDatas();
    protected abstract RowMap getMainData();
    protected void perist(HistoryTransContext historyTransContext) {
        ProcessCreateContext processCreateContext = historyTransContext.getProcessCreateContext();
        Connection connection = DBSql.open();
        boolean isProcessPersist = false;
        try {
            connection.setAutoCommit(false);
            ProcessPersistBiz.getInstance().persistProcess(connection, historyTransContext);
            ProcessPersistBiz.getInstance().persistComment(connection, historyTransContext);
            ProcessPersistBiz.getInstance().persistTask(connection, historyTransContext);
            connection.commit();
            isProcessPersist = true;
            ProcessPersistBiz.getInstance().persistBO(connection, historyTransContext);
            connection.commit();
            ProcessPersistBiz.getInstance().persistFile(connection, historyTransContext);
            ProcessPersistBiz.getInstance().insertHistoryTransLog(connection, historyTransContext.getHistoryTransLogModel());
            for(HistoryTransLogModel historyTransLogModel: historyTransContext.getHistoryTransLogModelList()){
                ProcessPersistBiz.getInstance().insertHistoryTransLog(connection, historyTransLogModel);
            }
            connection.commit();
        } catch (Exception e) {
            SDK.getLogAPI().consoleInfo("是否进入异常报错==============");
            SDK.getLogAPI().consoleInfo("=============="+ historyTransContext.getHistoryTransLogModel().getOuterPK());
            e.printStackTrace();
            try {
                connection.rollback();
            } catch (SQLException e1) {
                SDK.getLogAPI().consoleInfo("回滚异常==============");
                e1.printStackTrace();
            }
            if (isProcessPersist) {
                String id = historyTransContext.getProcessCreateContext().getProcessInstanceModel().getId();
                try {
                   // SDK.getProcessAPI().deleteById(id, "admin");
                }catch (Exception e2){
                    SDK.getLogAPI().consoleInfo("删除流程异常==============");
                    e2.printStackTrace();
                }
            }
            BO errLogBO = new BO();
            HistoryTransLogModel historyTransLogModel = historyTransContext.getHistoryTransLogModel();
            errLogBO.set("BPM_BOID", historyTransLogModel.getBpmBOId());
            errLogBO.set("BPM_EXT1", historyTransLogModel.getBpmExt1());
            errLogBO.set("BPM_EXT2", historyTransLogModel.getBpmExt2());
            errLogBO.set("BPM_TABLE", historyTransLogModel.getBpmTable());
            errLogBO.set("OUTER_TABLE", historyTransLogModel.getOuterTable());
            errLogBO.set("OUTER_PK", historyTransLogModel.getOuterPK());
            errLogBO.set("OUTER_EXT1", historyTransLogModel.getOuterExt1());
            errLogBO.set("OUTER_EXT2", historyTransLogModel.getOuterExt2());
            errLogBO.set("OUTER_EXT3", historyTransLogModel.getOuterExt3());
            try (StringWriter sw = new StringWriter()) {
                PrintWriter pw = new PrintWriter(sw);
                e.printStackTrace(pw);
                sw.flush();
                String errorLog = sw.toString();
                if (errorLog.length() > 3999) {
                    errorLog = StringUtils.substring(errorLog, 0, 3999);
                }
                errLogBO.set("ERRORLOG", errorLog);
            } catch (Exception e3) {
                e3.printStackTrace();
            }
            SDK.getBOAPI().createDataBO("BO_EU_HISTRANS_ERR_LOG", errLogBO, UserContext.fromUID("admin"));

        } finally {
            try {
                connection.setAutoCommit(true);
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }

}
